package com.ljz.dao.impl;

import com.ljz.dao.UserDao;
import com.ljz.entity.User;
import com.ljz.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {

    @Override
    public User findByUid(Integer uid) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        User user = null;
        conn = DBUtil.getConn();
        String sql = "select * from user where uid=?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1,uid);
            rs = ps.executeQuery();
            if(rs.next()){
                user = new User();
                user.setUid(uid);
                user.setUname(rs.getString("uname"));
                user.setPassword(rs.getString("password"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeAll(conn,ps,rs);
        }
        return user;
    }

    @Override
    public User findByUname(String uname) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        User user = null;
        conn = DBUtil.getConn();
        String sql = "select * from user where uname=?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,uname);
            rs = ps.executeQuery();
            if(rs.next()){
                user = new User();
                user.setUname(uname);
                user.setPassword(rs.getString("password"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeAll(conn,ps,rs);
        }
        return user;
    }

    @Override
    public boolean addUser(User user) {
        boolean flag = false;
        Connection conn = conn = DBUtil.getConn();
        PreparedStatement ps = null;

        String sql = "insert into user(uname,password) values(?,?)";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,user.getUname());
            ps.setString(2,user.getPassword());
            int i = ps.executeUpdate();
            if(i>0){
                flag = true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.closeAll(conn,ps,null);
        }
        return flag;
    }

    @Override
    public boolean checkName(String uname) {
        String sql = "select uname from user where uname=?";
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        boolean flag = false;
        conn = DBUtil.getConn();
        try{
            ps = conn.prepareStatement(sql);
            ps.setString(1,uname);
            rs = ps.executeQuery();
            if(rs.next()){
                flag = true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.closeAll(conn,ps,rs);
        }
        return flag;
    }
}
